home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Security / Hotspot Shield 1.08 / HSS-1.08-install-anchorfree-76-conduit.exe / components / Conduit.js next >
Text File  |  2008-06-26  |  13KB  |  404 lines

  1. function Fix404Obj(strUrl, strCTID)
  2. {
  3.     this.url = strUrl;
  4.     this.CTID = strCTID;
  5. }
  6.  
  7. //helper functions
  8. function GetMostRecentBrowserWindow()
  9. {
  10.     var objWindowsManager = Components.classes['@mozilla.org/appshell/window-mediator;1']
  11.                     .getService(Components.interfaces.nsIWindowMediator);
  12.     
  13.     var objRecentBrowserWindow = objWindowsManager.getMostRecentWindow('navigator:browser');    
  14.     return     objRecentBrowserWindow;
  15. }
  16.  
  17. var EBServerDataURL = 
  18. {
  19.     ServerRequest : function(strURL,strPostData,strUserName,strPassword,ServerResponseFunction)
  20.     {
  21.         var objIOService    = Components.classes["@mozilla.org/network/io-service;1"].createInstance(Components.interfaces.nsIIOService);
  22.         var objURI            = objIOService.newURI(strURL, null, null);
  23.         
  24.         if(strUserName != null && strPassword != null)
  25.         {
  26.             objURI.username        = strUserName;
  27.             objURI.password        = strPassword;
  28.         }
  29.         
  30.         var objChannel        = objIOService.newChannelFromURI(objURI);
  31.         
  32.         objChannel.QueryInterface(Components.interfaces.nsIHttpChannel).setRequestHeader('PRAGMA','NO-CACHE',false);
  33.         objChannel.QueryInterface(Components.interfaces.nsIHttpChannel).setRequestHeader('CACHE-CONTROL','NO-CACHE',false);
  34.  
  35.         
  36.         if(strPostData != null) 
  37.         {
  38.             var objUploadStream        = Components.classes["@mozilla.org/io/string-input-stream;1"].createInstance(Components.interfaces.nsIStringInputStream);
  39.             objUploadStream.setData(strPostData, strPostData.length);
  40.               
  41.             var objUploadChannel    = objChannel.QueryInterface(Components.interfaces.nsIUploadChannel);
  42.             objUploadChannel.setUploadStream(objUploadStream, "application/x-www-form-urlencoded", -1);
  43.               
  44.             objChannel.QueryInterface(Components.interfaces.nsIHttpChannel).requestMethod = "POST";
  45.         }
  46.         
  47.         var objObserver = new this.Observer(ServerResponseFunction);
  48.         objChannel.asyncOpen(objObserver, null);
  49.     },
  50.     
  51.     Observer: function(ServerResponseFunction)
  52.     {
  53.         return ({
  54.                     Data : "",
  55.                     
  56.                     onStartRequest: function(aRequest, aContext)
  57.                     {
  58.                         this.Data = "";
  59.                     },
  60.                     
  61.                     onStopRequest: function(aRequest, aContext, aStatus)
  62.                     {
  63.                         if(ServerResponseFunction)
  64.                         {
  65.                             ServerResponseFunction(this.Data, aRequest);
  66.                         }
  67.                     },
  68.                         
  69.                     onDataAvailable: function(aRequest, aContext, aStream, aSourceOffset, aLength)
  70.                     {
  71.                         var objScriptableInputStream = Components.classes["@mozilla.org/scriptableinputstream;1"].createInstance(Components.interfaces.nsIScriptableInputStream);
  72.                         objScriptableInputStream.init(aStream);
  73.                         this.Data += objScriptableInputStream.read(aLength);
  74.                     }
  75.                 });
  76.     }
  77. }
  78.  
  79. //Browser Progress Listener
  80. var XPCOMEBBrowserProgressListener =
  81. {
  82.   QueryInterface: function(aIID)
  83.   {
  84.    if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
  85.        aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
  86.        aIID.equals(Components.interfaces.nsISupports))
  87.      return this;
  88.    throw Components.results.NS_NOINTERFACE;
  89.   },
  90.   
  91.   onStateChange: function(aProgress, aRequest, aFlag, aStatus)
  92.   { 
  93.        
  94.        var strURL = '';
  95.        
  96.        if(aRequest)
  97.             strURL = aRequest.name;
  98.        
  99.         
  100.        if(aFlag & Components.interfaces.nsIWebProgressListener.STATE_STOP)
  101.        {
  102.             
  103.             var bIsSuccess  = Components.isSuccessCode(aStatus);
  104.             var responseStatus = 200;
  105.             var iContentLength = 0;
  106.             try
  107.             {
  108.                 httpChannel = aRequest.QueryInterface(Components.interfaces.nsIHttpChannel);
  109.                 responseStatus = httpChannel.responseStatus;
  110.                 iContentLength = aRequest.QueryInterface(Components.interfaces.nsIChannel).contentLength;
  111.                 
  112.             }
  113.             catch(ex){}
  114.             
  115.             var oWindow = aProgress.DOMWindow;
  116.             
  117.             var objConduit = Components.classes["@conduit.com/helper;3"]
  118.                                     .getService(Components.interfaces.nsIConduit);
  119.                 
  120.             var strRedirectUrl = objConduit.retrieve404redirectUrl();
  121.             
  122.             var strPreservedUrl = oWindow.document.baseURI;
  123.                         
  124.             //executes when 404 custom page is received
  125.             var oCallback = function(strResponse)
  126.             {
  127.                 //check if the page is legal
  128.                 if(strResponse.substring(0,35).indexOf("<!--FIX_PAGE_NOT_FOUND_REMARK-->") == -1)
  129.                     return;
  130.                     
  131.                 try
  132.                 {
  133.                     strResponse = strResponse.replace(/<!--FIX_PAGE_NOT_FOUND_REMARK-->/,'');
  134.                     strResponse = strResponse.replace(/<!--/g,'');
  135.                     strResponse = strResponse.replace(/-->/g,'');
  136.                     strResponse = strResponse.replace(/\'/g,"\\'");
  137.                     strResponse = strResponse.replace(/\r\n/g,"");
  138.                     strResponse = strResponse.replace(/\/\/\<\!\[CDATA\[/g,'');
  139.                     strResponse = strResponse.replace(/\/\/]]>/g,'');
  140.                     
  141.                     var strJSInnerHTML = "document.write('"+strResponse+"')";
  142.  
  143.                     var oScript = oWindow.document.createElement('script');
  144.                     oScript.setAttribute('type','text/javascript');
  145.                     oScript.innerHTML = strJSInnerHTML;
  146.                     
  147.                     oWindow.document.getElementsByTagName('head')[0].appendChild(oScript);
  148.                 }
  149.                 catch(e) {}
  150.             };
  151.             
  152.             //handle 404 error
  153.             //if(responseStatus == 404 || !Components.isSuccessCode(aStatus))
  154.             if(responseStatus == 404 && iContentLength <= 1635)
  155.             {
  156.                 if(strRedirectUrl)
  157.                 {
  158.                     try
  159.                     {
  160.                         aRequest.cancel(aRequest.NS_ERROR_FAILURE);
  161.                         strRedirectUrl = strRedirectUrl.replace(/EB_MAIN_FRAME_URL/g, strPreservedUrl);
  162.                         EBServerDataURL.ServerRequest(strRedirectUrl, null, null, null, oCallback);
  163.                     }
  164.                     catch(ex) {}
  165.                 }
  166.             }
  167.         
  168.        }
  169.       
  170.       return 0;
  171.   },
  172.   onLocationChange: function(aProgress, aRequest, aURI) {return 0;},
  173.   onProgressChange: function() {return 0;},
  174.   onStatusChange: function() {return 0;},
  175.   onSecurityChange: function(aProgress, aRequest, aState) {return 0;},
  176.   onLinkIconAvailable: function() {return 0;}
  177. }
  178.  
  179. //XPCOM
  180. function ConduitHelperObject() {
  181.     this.Data = new Array();
  182.     this.RedirectArr = new Array();
  183.     this.isListenerAttached = false;
  184. }
  185.  
  186. ConduitHelperObject.prototype = {
  187.     
  188.     //Global key - value storage
  189.     storeKey : function(strCTID, strKey, strValue)
  190.     {
  191.         if(!this.Data[strCTID])
  192.             this.Data[strCTID] = new Array();
  193.             
  194.         this.Data[strCTID][strKey] = strValue;
  195.     },
  196.     
  197.     retrieveKey : function(strCTID, strKey)
  198.     {
  199.         return this.Data[strCTID][strKey];
  200.     },
  201.     
  202.     deleteKey : function(strCTID, strKey)
  203.     {
  204.         delete this.Data[strCTID][strKey];
  205.     },
  206.     
  207.     // Fix not found page functions
  208.     registerSingleWindow404redirectUrl : function(strUrl, strCTID)
  209.     {
  210.         this._addUrlToList(strUrl, strCTID);
  211.         this._addProgressListener(false);
  212.     },
  213.     
  214.     unregisterSingleWindow404redirectUrl : function(strUrl, strCTID)
  215.     {
  216.         this._removeUrlFromList(strUrl, strCTID);
  217.         if(this.RedirectArr.length == 0)
  218.             this._removeProgressListener(false);
  219.     },
  220.     
  221.     registerAllWindows404redirectUrl : function(strUrl, strCTID)
  222.     {
  223.         this._addUrlToList(strUrl, strCTID);
  224.         this._addProgressListener(true);
  225.     },
  226.     
  227.     unregisterAllWindows404redirectUrl : function(strUrl, strCTID)
  228.     {
  229.         this._removeUrlFromList(strUrl, strCTID);
  230.         if(this.RedirectArr.length == 0)
  231.              this._removeProgressListener(true);
  232.     },
  233.     
  234.     retrieve404redirectUrl : function()
  235.     {
  236.         if(this.RedirectArr.length == 0)
  237.             return "";
  238.         else
  239.             return this.RedirectArr[this.RedirectArr.length-1].url;
  240.     },
  241.     
  242.     _addUrlToList : function(strUrl, strCTID)
  243.     {
  244.         if(this.RedirectArr.length == 0)
  245.             this.RedirectArr.push(new Fix404Obj(strUrl, strCTID));
  246.         else
  247.         {
  248.             var bIsFound = false;
  249.             
  250.             for(var i=0; i<this.RedirectArr.length; i++)
  251.                 if(this.RedirectArr[i].CTID == strCTID)
  252.                 {
  253.                     bIsFound = true;
  254.                     //update the url
  255.                     this.RedirectArr[i].url = strUrl;
  256.                     break;
  257.                 }
  258.             
  259.             if(!bIsFound)
  260.                 this.RedirectArr.push(new Fix404Obj(strUrl, strCTID));
  261.         }
  262.     },
  263.     
  264.     _removeUrlFromList : function(strUrl, strCTID)
  265.     {
  266.         if(this.RedirectArr.length == 0)
  267.             return;
  268.         
  269.         var iIndexToRemove = -1;
  270.         for(var i=0; i<this.RedirectArr.length; i++)
  271.             if(this.RedirectArr[i].CTID == strCTID)
  272.             {
  273.                 iIndexToRemove = i;
  274.                 break;
  275.             }
  276.  
  277.         if(iIndexToRemove != -1)
  278.             this.RedirectArr.splice(iIndexToRemove, 1);
  279.     },
  280.     
  281.     _addProgressListener : function(bIsAll)
  282.     {
  283.         if(bIsAll)
  284.         {
  285.             var objWindowsManager = Components.classes['@mozilla.org/appshell/window-mediator;1']
  286.                             .getService(Components.interfaces.nsIWindowMediator);
  287.             
  288.             var objEnumerator = objWindowsManager.getEnumerator('navigator:browser');    
  289.             while(objEnumerator.hasMoreElements()) 
  290.             {
  291.                 var oWindow = objEnumerator.getNext();
  292.                 if(oWindow.getBrowser)
  293.                 {
  294.                     try 
  295.                     {
  296.                         if(!oWindow.getBrowser().hasAttribute('EBMainListenerSet'))
  297.                         {
  298.                             oWindow.getBrowser().setAttribute('EBMainListenerSet',true);
  299.                             oWindow.getBrowser().addProgressListener(XPCOMEBBrowserProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_STOP);
  300.                         }
  301.                     }    
  302.                     catch(ex){}
  303.                 }
  304.             }
  305.         }
  306.         else
  307.         {
  308.             var oWindow = GetMostRecentBrowserWindow();
  309.             if(!oWindow.getBrowser().hasAttribute('EBMainListenerSet'))
  310.             {
  311.                 oWindow.getBrowser().setAttribute('EBMainListenerSet',true);
  312.                 oWindow.getBrowser().addProgressListener(XPCOMEBBrowserProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_STOP);
  313.             }
  314.         }
  315.     },
  316.     
  317.      _removeProgressListener : function(bIsAll)
  318.     {
  319.         if(bIsAll)
  320.         {
  321.             var objWindowsManager = Components.classes['@mozilla.org/appshell/window-mediator;1']
  322.                             .getService(Components.interfaces.nsIWindowMediator);
  323.             
  324.             var objEnumerator = objWindowsManager.getEnumerator('navigator:browser');    
  325.             while(objEnumerator.hasMoreElements()) 
  326.             {
  327.                 var oWindow = objEnumerator.getNext();
  328.                 if(oWindow.getBrowser)
  329.                 {
  330.                     try 
  331.                     {
  332.                         if(oWindow && oWindow.getBrowser().hasAttribute('EBMainListenerSet'))
  333.                         {
  334.                             oWindow.getBrowser().removeAttribute('EBMainListenerSet');
  335.                             oWindow.getBrowser().removeProgressListener(XPCOMEBBrowserProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_STOP);
  336.                         }
  337.                     }    
  338.                     catch(ex){}
  339.                 }
  340.             }
  341.         }
  342.         else
  343.         {
  344.             var oWindow = GetMostRecentBrowserWindow();
  345.             if(oWindow && oWindow.getBrowser().hasAttribute('EBMainListenerSet'))
  346.             {
  347.                 oWindow.getBrowser().removeAttribute('EBMainListenerSet');
  348.                 oWindow.getBrowser().removeProgressListener(XPCOMEBBrowserProgressListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_STOP);
  349.             }
  350.         }
  351.     },
  352.     
  353.     QueryInterface: function (uuid) {
  354.         if (uuid.equals(Components.interfaces.nsIConduit) ||
  355.             uuid.equals(Components.interfaces.nsISupports)) {
  356.             return this;
  357.         }
  358.         
  359.         Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  360.         return null;
  361.     },
  362. }
  363.  
  364. const COMPONENT_ID = Components.ID("{B009C480-CA54-4483-BA59-5505DD98BE40}");
  365.  
  366. var ConduitHelperObjectModule = {
  367.     registerSelf: function (compMgr, fileSpec, location, type) {
  368.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  369.         compMgr.registerFactoryLocation(COMPONENT_ID,
  370.                                         "Conduit Helper Object",
  371.                                         "@conduit.com/helper;3",
  372.                                         fileSpec,
  373.                                         location,
  374.                                         type);
  375.     },
  376.  
  377.     getClassObject: function (compMgr, cid, iid) {
  378.         if (!cid.equals(COMPONENT_ID))
  379.             throw Components.results.NS_ERROR_NO_INTERFACE;
  380.         if (!iid.equals(Components.interfaces.nsIFactory))
  381.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  382.  
  383.         return ConduitHelperObjectFactory;
  384.     },
  385.  
  386.     canUnload: function(compMgr) {
  387.         return true;
  388.     }
  389. };
  390.  
  391. var ConduitHelperObjectFactory = {
  392.     createInstance: function (outer, iid) {
  393.         if (outer != null)
  394.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  395.         return new ConduitHelperObject().QueryInterface(iid);
  396.     }
  397. };
  398.  
  399. function NSGetModule(compMgr, fileSpec) {
  400.     return ConduitHelperObjectModule;
  401. }
  402.  
  403.  
  404.